package com.yanqu.road.server.manger.activity.biaoju;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.biaoju.config.*;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.activity.biaoju.BiaoJuConfig;
import com.yanqu.road.logic.thread.ThreadTaskManger;
import com.yanqu.road.pb.activity.BiaoJuProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.biaoju.ActivityBiaoJuModule;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.executor.GeneralTask;

import java.util.List;
import java.util.Map;

public class BiaoJuConfigMgr extends TempMgr {
    private static BiaoJuConfig config;
    private static ThreadTaskManger threadTaskManger;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        if(null == threadTaskManger) {
            threadTaskManger = new ThreadTaskManger(1, "BiaoJuMgrTask");
        }
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void reloadBiaoJuConfig() {
        getLogger().info("reloadBiaoJuConfig start");
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.BiaoJu.getValue());
        if (openActivityInfoList == null || openActivityInfoList.isEmpty()) {
            config = null;
            getLogger().info("reloadBiaoJuConfig openActivityInfoList is empty");
            return;
        }

        ActivityInfo activityInfo = openActivityInfoList.get(0);
        Map<String, ActivityConfig> activityConfigMap = ActivityMgr.getActivityConfigMap(activityInfo.getActivityId());
        BiaoJuConfig tempConfig = new BiaoJuConfig(activityInfo, activityConfigMap);

        config = tempConfig;

        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            player.getModule(ActivityBiaoJuModule.class).initSystemOpen();
        }

        getLogger().info("reloadBiaoJuConfig end");
    }

    public static BiaoJuConfig getConfig() {
        return config;
    }

    public static BiaoJuProto.BiaoJuSyncConfigMsg.Builder getConfigMsg(String language) {
        if (config == null) {
            return null;
        }

        BiaoJuProto.BiaoJuSyncConfigMsg.Builder configMsg = config.getConfigMsg(language);
        if (configMsg == null) {
            synchronized (config) {
                configMsg = config.getConfigMsg(language);
                if (configMsg == null) {
                    configMsg = parseConfigMsg(language);
                    config.setConfigMsg(language, configMsg);
                }
            }
        }
        return configMsg;
    }

    private static BiaoJuProto.BiaoJuSyncConfigMsg.Builder parseConfigMsg(String language) {
        BiaoJuProto.BiaoJuSyncConfigMsg.Builder builder = BiaoJuProto.BiaoJuSyncConfigMsg.newBuilder();
        builder.setActivityId(config.getActivityId());

        for (BiaoJuDartConfig dartConfig : config.getDartConfigMap().values()) {
            BiaoJuProto.BiaoJuConfigDartTemp.Builder dartBuilder = BiaoJuProto.BiaoJuConfigDartTemp.newBuilder();
            dartBuilder.setId(dartConfig.getId());
            dartBuilder.setName(ServerLanguageMgr.getContent(dartConfig.getName(), language));
            dartBuilder.setDesc(ServerLanguageMgr.getContent(dartConfig.getDesc(), language));
            dartBuilder.setIcon(dartConfig.getIcon());
            dartBuilder.setQuality(dartConfig.getQuality());
            dartBuilder.setMonsterPower(dartConfig.getMonsterPower());
            dartBuilder.setMonsterTime(dartConfig.getMonsterTime());
            dartBuilder.setWeights(dartConfig.getWeights());
            dartBuilder.setIncomeScoreSuccess(dartConfig.getIncomeScoreSuccess());
            dartBuilder.setIncomeScoreHalf(dartConfig.getIncomeScoreHalf());
            dartBuilder.setIncomeScoreFail(dartConfig.getIncomeScoreFail());
            dartBuilder.setIncomeBountySuccess(dartConfig.getIncomeBountySuccess());
            dartBuilder.setIncomeBountyHalf(dartConfig.getIncomeBountyHalf());
            dartBuilder.setIncomeBountyFail(dartConfig.getIncomeBountyFail());
            dartBuilder.setGear(dartConfig.getGear());
            builder.addDartList(dartBuilder);
        }

        for (BiaoJuEventConfig eventConfig : config.getEventConfigList()) {
            BiaoJuProto.BiaoJuConfigEventTemp.Builder eventBuilder = BiaoJuProto.BiaoJuConfigEventTemp.newBuilder();
            eventBuilder.setId(eventConfig.getId());
            eventBuilder.setName(ServerLanguageMgr.getContent(eventConfig.getName(), language));
            eventBuilder.setDesc(ServerLanguageMgr.getContent(eventConfig.getDesc(), language));
            builder.addEventList(eventBuilder);
        }

        for (BiaoJuCityConfig cityConfig : config.getCityConfigMap().values()) {
            BiaoJuProto.BiaoJuConfigCityTemp.Builder cityBuilder = BiaoJuProto.BiaoJuConfigCityTemp.newBuilder();
            cityBuilder.setId(cityConfig.getId());
            cityBuilder.setName(ServerLanguageMgr.getContent(cityConfig.getName(), language));
            cityBuilder.setDesc(ServerLanguageMgr.getContent(cityConfig.getDesc(), language));
            cityBuilder.setIcon(cityConfig.getIcon());
            cityBuilder.setPicPos(cityConfig.getPicPos());
            cityBuilder.setPicSize(cityConfig.getPicSize());
            cityBuilder.setTitlePos(cityConfig.getTitlePos());
            cityBuilder.setEventType(cityConfig.getEventType());
            builder.addCityList(cityBuilder);
        }

        for (BiaoJuTitleConfig titleConfig : config.getTitleConfigMap().values()) {
            BiaoJuProto.BiaoJuConfigTitleTemp.Builder titleBuilder = BiaoJuProto.BiaoJuConfigTitleTemp.newBuilder();
            titleBuilder.setId(titleConfig.getId());
            titleBuilder.setName(ServerLanguageMgr.getContent(titleConfig.getName(), language));
            titleBuilder.setDesc(ServerLanguageMgr.getContent(titleConfig.getDesc(), language));
            titleBuilder.setIcon(titleConfig.getIcon());
            titleBuilder.setCost(titleConfig.getCost());
            titleBuilder.setIncomeAdd(titleConfig.getIncomeAdd());
            titleBuilder.setPower(titleConfig.getPower());
            titleBuilder.setWeights(titleConfig.getWeights());
            titleBuilder.setRefreshNum(titleConfig.getRefreshNum());
            titleBuilder.setHighDartParam(titleConfig.getHighDartParam());
            builder.addTitleList(titleBuilder);
        }

        for (BiaoJuCityRankConfig cityRankConfig : config.getCityRankAdditionList()) {
            BiaoJuProto.BiaoJuConfigCityRankTemp.Builder cityRankBuilder = BiaoJuProto.BiaoJuConfigCityRankTemp.newBuilder();
            cityRankBuilder.setId(cityRankConfig.getId());
            cityRankBuilder.setRank(cityRankConfig.getRank());
            cityRankBuilder.setIncomeAdd(cityRankConfig.getIncomeAdd());
            builder.addCityRankList(cityRankBuilder);
        }

        for (BiaoJuLevelConfig levelConfig : config.getLevelConfigMap().values()) {
            BiaoJuProto.BiaoJuConfigLevelTemp.Builder levelBuilder = BiaoJuProto.BiaoJuConfigLevelTemp.newBuilder();
            levelBuilder.setId(levelConfig.getId());
            levelBuilder.setCost(levelConfig.getCost());
            levelBuilder.setIncomeAdd(levelConfig.getIncomeAdd());
            builder.addLevelList(levelBuilder);
        }

        return builder;
    }

    public static void addTask(GeneralTask task) {
        threadTaskManger.addTask(0, task);
    }
}
